﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class CompanyReportController : System.Web.Mvc.Controller
    {
        //
        // GET: /CompanyReport/
        /// <summary>
        /// 报表
        /// </summary>
        /// <param name="uid">querymonth+queryyear</param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public ActionResult Index(string pdate)
        {
            if (BenefitHelper.Sys.Company.HasInited(db))
            {
                ViewBag.company = db.Company.First();
            }
            else
            {
                ViewBag.company = new BenefitHelper.Sys.Company();
            }
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            int year = 0;
            int month = 0;
            if (pdate.Length == 4)//从年转过来的
            {
                year = Convert.ToInt32(pdate.Substring(0, 4));
                month = DateTime.Now.Month;
            }
            else
            {
                year = Convert.ToInt32(pdate.Substring(0, 4));
                month = Convert.ToInt32(pdate.Substring(4));
            }

            //  ViewBag.datequery = cookie.GetCookie("querydate");
            ViewBag.queryyear = year;
            ViewBag.querymonth = month;
            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
            th = th.GetMonthLastTradingDay(year, month, db);
            ViewBag.queryweek = th.WPdate;
            ViewBag.queryId = th.Id;
            return View();
        }

        /// <summary>
        /// 周报表
        /// </summary>
        /// <param name="uid">queryweek</param>
        /// <param name="pdate">queryyear</param>
        /// <returns></returns>
        public ActionResult SummaryIncome(int uid, int pdate)
        {
            List<BenefitHelper.Sys.Groups> groups = new BenefitHelper.Sys.Groups().GetGroups(db);
            List<BenefitHelper.Data.Week.GroupWeekSum> groupweeksums = new List<BenefitHelper.Data.Week.GroupWeekSum>();
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                BenefitHelper.Data.Week.GroupWeekSum groupweeksum = new BenefitHelper.Data.Week.GroupWeekSum().GetGroupWeekSum(group.Id, uid, pdate, db);
                groupweeksums.Add(groupweeksum);
            }
            ViewBag.groups = groups;
            List<BenefitHelper.View.OperatorWeekSum> operatorweeksums = new List<BenefitHelper.View.OperatorWeekSum>();
            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
            th = th.GetWeekLastTradingDay(uid, pdate, db);
            ViewBag.weekend = th;
            var weekbegin = th.GetWeekFirstTradingDay(uid, pdate, db);
            ViewBag.weekbegin = weekbegin;

            List<KeyValuePair<int, double>> groupMoney = new List<KeyValuePair<int, double>>();
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(group.Id, th.GetPdateFromTradeHistoryId(th.Id, db), db);
                //   
                double groupsum = 0;
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorDayMoney money = new BenefitHelper.View.OperatorDayMoney().GetOperatorDayMoney(op.Id, weekbegin.Id, db);
                    groupsum += money.PresetTlePosit;
                }
                KeyValuePair<int, double> kp = new KeyValuePair<int, double>(group.Id, groupsum);
                groupMoney.Add(kp);
            }
            ViewBag.groupMoney = groupMoney;
            List<BenefitHelper.Sys.DayGroupOperators> ogos = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(th.GetPdateFromTradeHistoryId(th.Id, db), db);
            ViewBag.ogos = ogos;
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(group.Id, th.GetPdateFromTradeHistoryId(th.Id, db), db);
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorWeekSum sum = new BenefitHelper.View.OperatorWeekSum().GetOperatorWeekSum(op.Id, pdate, th.MPdate, uid, db);
                    operatorweeksums.Add(sum);
                }
            }
            ViewBag.operatorweeksums = operatorweeksums;
            ViewBag.groupweeksums = groupweeksums;
            ViewBag.daytradeingcount = th.GetWeekTradeCount(uid, pdate, db);
            ViewBag.billpic = BenefitHelper.Tools.ChartAtManager.GroupBillPic(th.WPdate, th.YPdate, db);
            return View();
        }
        /// <summary>
        /// 日报表
        /// </summary>
        /// <param name="uid">queryweek</param>
        /// <param name="pdate">queryyear</param>
        /// <returns></returns>
        public ActionResult SummaryDayIncome(int id)
        {
            List<BenefitHelper.Sys.Groups> groups = new BenefitHelper.Sys.Groups().GetGroups(db);
            List<BenefitHelper.Data.Day.GroupDayChange> groupdaysums = new List<BenefitHelper.Data.Day.GroupDayChange>();
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                BenefitHelper.Data.Day.GroupDayChange groupweeksum = new BenefitHelper.Data.Day.GroupDayChange().GetGroupDayChange(group.Id, id, db);
                groupdaysums.Add(groupweeksum);
            }
            ViewBag.groups = groups;
            List<BenefitHelper.View.OperatorDayChange> operatorweeksums = new List<BenefitHelper.View.OperatorDayChange>();
            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
            th = th.GetModel(id, db);
            // ViewBag.weekend = th;
            //  var weekbegin = th.GetWeekFirstTradingDay(uid, pdate, db);
            //  ViewBag.weekbegin = weekbegin;
            ViewBag.queryday = th.YPdate + "年" + th.MPdate + "月" + th.DPdate + "日";
            List<KeyValuePair<int, double>> groupMoney = new List<KeyValuePair<int, double>>();
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(group.Id, th.GetPdateFromTradeHistoryId(th.Id, db), db);
                //   
                double groupsum = 0;
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorDayMoney money = new BenefitHelper.View.OperatorDayMoney().GetOperatorDayMoney(op.Id, th.Id, db);
                    groupsum += money.PresetTlePosit;
                }
                KeyValuePair<int, double> kp = new KeyValuePair<int, double>(group.Id, groupsum);
                groupMoney.Add(kp);
            }
            ViewBag.groupMoney = groupMoney;
            List<KeyValuePair<int, double>> groupMoney2 = new List<KeyValuePair<int, double>>();
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(group.Id, th.GetPdateFromTradeHistoryId(th.Id, db), db);
                //   
                double groupsum = 0;
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorDayMoney money = new BenefitHelper.View.OperatorDayMoney().GetOperatorDayMoney(op.Id, th.Id, db);
                    groupsum += money.SettlePosit;
                }
                KeyValuePair<int, double> kp = new KeyValuePair<int, double>(group.Id, groupsum);
                groupMoney2.Add(kp);
            }
            ViewBag.groupMoney2 = groupMoney2;
            List<BenefitHelper.View.OperatorDayPosition> operatordaypositions = new List<BenefitHelper.View.OperatorDayPosition>();
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(group.Id, th.GetPdateFromTradeHistoryId(th.Id, db), db);
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    List<BenefitHelper.View.OperatorDayPosition> money = new BenefitHelper.View.OperatorDayPosition().GetAllAccountDayPositionDelivery(th.Id, op.Id, db);
                    foreach (BenefitHelper.View.OperatorDayPosition m in money)
                    {
                        m.Investorid = group.Id.ToString();
                        operatordaypositions.Add(m);
                    }
                }
            }
            ViewBag.groupMoney3 = operatordaypositions;
            List<BenefitHelper.Sys.DayGroupOperators> ogos = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(th.GetPdateFromTradeHistoryId(th.Id, db), db);
            ViewBag.ogos = ogos;
            foreach (BenefitHelper.Sys.Groups group in groups)
            {
                List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(group.Id, th.GetPdateFromTradeHistoryId(th.Id, db), db);
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorDayChange sum = new BenefitHelper.View.OperatorDayChange().GetOperatorDayChange(op.Id, id, db);
                    operatorweeksums.Add(sum);
                }
            }
            ViewBag.operatorweeksums = operatorweeksums;
            ViewBag.groupweeksums = groupdaysums;
            //  ViewBag.daytradeingcount = th.GetWeekTradeCount(uid, pdate, db);
            ViewBag.billpic = BenefitHelper.Tools.ChartAtManager.GroupBillPic(th.Id, db);
            return View();
        }
        [HttpPost]
        public ActionResult DownLoadDayReport(int id)
        {
            if (Request.IsAjaxRequest())
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + @"pdf\" + id + @"\" + DateTime.Now.Millisecond + "" + id + ".pdf";
                new BenefitHelper.Tools.PDFHelper().Write(id, path);
                return Content(path.Replace(AppDomain.CurrentDomain.BaseDirectory, "/"));
            }
            else
            {
                return Content("-1");
            }
        }

        /// <summary>
        /// 风险统计
        /// </summary>
        /// <returns></returns>
        public ActionResult RiskSummary()
        {
            return View();
        }
        /// <summary>
        /// 账户余额
        /// </summary>
        /// <returns></returns>
        public ActionResult AccountLeft()
        {
            return View();
        }
        /// <summary>
        /// 月度盈亏一览表
        /// </summary>
        /// <returns></returns>
        public ActionResult MonthSummary()
        {
            return View();
        }
        /// <summary>
        /// 罚款记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Publish()
        {
            return View();
        }
        /// <summary>
        /// 月度统计
        /// </summary>
        /// <returns></returns>
        public ActionResult GroupMonthSummary(int uid, int pdate)
        {
            //List<BenefitHelper.Sys.Groups> groups = new BenefitHelper.Sys.Groups().GetGroups(db);
            //ViewBag.groups = groups;
            //List<BenefitHelper.Data.Month.GroupMonthSum> groupmonthsums = new BenefitHelper.Data.Month.GroupMonthSum().GetGroupMonthSum(uid, pdate, db);
            //ViewBag.groupmonthsums = groupmonthsums;

            BenefitHelper.Data.Month.CompanyMonthSum companymonthsum = new BenefitHelper.Data.Month.CompanyMonthSum().GetCompanyMonthSum(uid, pdate, db);
            ViewBag.companymonthsum = companymonthsum;

            List<BenefitHelper.Data.Week.CompanyWeekSum> companyweeksums = new BenefitHelper.Data.Week.CompanyWeekSum().GetCompanyWeekSums(pdate, uid, db);
            ViewBag.companyweeksums = companyweeksums;

            List<BenefitHelper.View.OperatorMonthSum> operatormonthsums = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSums(uid, pdate, db);
            ViewBag.operatormonthsums = operatormonthsums;
            ViewBag.querymonth = uid;
            ViewBag.queryyear = pdate;
            return View();
        }

        public ActionResult CompanyProduct(int uid, int pdate)
        {
            return View();
        }
    }
}
